package edu.ucdavis.gwt.gis.client.state;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.ucdavis.gwt.gis.client.extras.CloseButton;

public class CloudSavePopup extends PopupPanel {

	private static SavePopupUiBinder uiBinder = GWT.create(SavePopupUiBinder.class);
	interface SavePopupUiBinder extends UiBinder<Widget, CloudSavePopup> {}

	public static CloudSavePopup INSTANCE = new CloudSavePopup();
	
	@UiField HTML createButton;
	@UiField HTML cancelButton;
	@UiField HTML messagePanel;
	@UiField CloseButton closeButton;
	@UiField HTML warningMessage;
	
	private String id = "";
	
	public CloudSavePopup() {
		setWidget(uiBinder.createAndBindUi(this));
		getElement().getStyle().setZIndex(8010);
		setGlassEnabled(true);
		setGlassStyleName("GwtGisPopup-glasspanel");
		
		warningMessage.setHTML("Url's can be shared for up to 30 days.  Once shared, save the map locally to continue edits.");
		
		createButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				createUrl();
			}
		});
		
		closeButton.addClickHandler(closeHandler);
		cancelButton.addClickHandler(closeHandler);
		
		Window.addResizeHandler(new ResizeHandler(){
			@Override
			public void onResize(ResizeEvent event) {
				if( isAttached() ) center();	
			}
		});
		

		
		addAttachHandler(new Handler(){
			@Override
			public void onAttachOrDetach(AttachEvent event) {
				if( event.isAttached() ) {
					messagePanel.setHTML("");
					center();
					createButton.setVisible(true);
					cancelButton.setText("Cancel");
					warningMessage.setWidth("250px");
				}
			}
		});
	}
	
	private void createUrl() {
		id = ClientStateManager.saveClientStateToCloud("", new CloudSaveHandler(){
			@Override
			public void onSaveComplete() {
				saveSuccess();
			}
		});
	}
	
	private void saveSuccess() {
		createButton.setVisible(false);
		cancelButton.setText("Close");
		warningMessage.setWidth("auto");
		
		String url = "http://"+Window.Location.getHost()+Window.Location.getPath()+"?id="+id;
		messagePanel.setHTML("<div style='padding-top:10px;color: green;text-align:center'>Success!</div><br />" +
				"<div style='color:#555555;text-align:center'>Copy and paste url below to share map:</div>" +
				"<div style='text-align:center;padding:15px;'><a href='"+url+"' target='_blank'>"+url+"</a></div>");
		
		center();
	}
	
	

	private ClickHandler closeHandler = new ClickHandler(){
		@Override
		public void onClick(ClickEvent event) {
			hide();
		}
	};

}
